<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="时间" prop="workDate">
        <el-date-picker
          v-model="queryParams.workDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createUserId">
        <el-select
          v-model="queryParams.createUserId"
          placeholder="请选择"
          style="width: 120px"
        >
          <el-option
            v-for="item in allList"
            :key="item.index"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工作类型" prop="workType">
        <el-select
          v-model="queryParams.workType"
          placeholder="请选择"
          style="width: 120px"
        >
          <el-option
            v-for="item in workTypelist"
            :key="item.index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择"
          style="width: 120px"
        >
          <el-option
            v-for="item in auditStatuslist"
            :key="item.index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['item:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    </el-form>

    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="120" />
      <el-table-column
        label="创建人"
        prop="createUserName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column prop="workType" label="工作类型" width="80">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.work_status"
            :value="scope.row.workType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="录入时间"
        align="center"
        prop="workDate"
        width="100"
      />
      <el-table-column prop="auditStatus" label="审核状态" width="80">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.work_type"
            :value="scope.row.auditStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="驳回原因"
        align="center"
        prop="auditFailCause"
        width="180"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.auditStatus == 1 ? true : false"
            size="mini"
            type="text"
            icon="el-icon-s-check"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['item:date:audit:work']"
            >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row)"
            v-hasPermi="['item:date:info']"
            >查看工作</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="当日工作列表"
      :visible.sync="openone"
      width="1280px"
      append-to-body
    >
      <el-table v-loading="loading" :data="childrenList">
        <el-table-column
          label="项目名字"
          prop="itemName"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="单位名字" align="center" prop="unitName" />
        <el-table-column label="单位价格" align="center" prop="unitPrice" />
        <el-table-column label="工作量" align="center" prop="workCount" />
        <el-table-column label="工时" align="center" prop="workTime" />
        <el-table-column label="工作内容" align="center" prop="workContent" />
        <el-table-column label="提成" align="center" prop="amountCommission" />
        <el-table-column label="社保" align="center" prop="amountSi" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
    <!-- 添加或修改角色配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-row>
        <el-col :span="24">
          <div v-if="itemAudit.auditStatus1 == 1" class="text">
            副经理：审核通过 时间：{{ itemAudit.auditTime1 }}
          </div>
          <div v-if="itemAudit.auditStatus1 == 2" class="text">
            副经理：审核驳回 时间：{{ itemAudit.auditTime1 }} 驳回原因：{{
              itemAudit.auditFailCause1
            }}
          </div>
          <div v-if="itemAudit.auditStatus2 == 1" class="text">
            总经理：审核通过 时间：{{ itemAudit.auditTime2 }}
          </div>
          <div v-if="itemAudit.auditStatus2 == 2" class="text">
            总经理：审核驳回 时间：{{ itemAudit.auditTime2 }} 驳回原因：{{
              itemAudit.auditFailCause2
            }}
          </div>
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="childrenList"
        style="margin-bottom: 20px"
      >
        <el-table-column
          label="项目名字"
          prop="itemName"
          :show-overflow-tooltip="true"
        />
        <el-table-column label="单位名字" align="center" prop="unitName" />
        <el-table-column label="工作量" align="center" prop="workCount" />
        <el-table-column label="工时" align="center" prop="workTime" />
        <el-table-column label="工作内容" align="center" prop="workContent" />
        <el-table-column label="提成" align="center" prop="amountCommission" />
        <el-table-column label="社保" align="center" prop="amountSi" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <el-form ref="form1" :model="form1" :rules="rules1" label-width="140px">
        <div v-hasRole="['fuzong']" v-if="itemAudit.auditStatus1 == 0">
          <el-row>
            <el-col :span="12">
              <el-form-item label="审核状态" prop="auditStatus1">
                <el-radio-group
                  v-model="form1.auditStatus1"
                  @change="audit($event, 1)"
                >
                  <el-radio
                    v-for="dict in auditList"
                    :key="dict.name"
                    :label="dict.id"
                    >{{ dict.name }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="驳回原因" prop="auditFailCause1">
                <el-input
                  v-model="form1.auditFailCause1"
                  placeholder="请输入驳回原因"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <el-form ref="form2" :model="form2" :rules="rules2" label-width="140px">
        <div v-hasRole="['manager']" v-if="itemAudit.auditStatus2 == 0">
          <el-row>
            <el-col :span="12">
              <el-form-item label="审核状态" prop="auditStatus2">
                <el-radio-group
                  v-model="form2.auditStatus2"
                  @change="audit($event, 2)"
                >
                  <el-radio
                    v-for="dict in auditList"
                    :key="dict.name"
                    :label="dict.id"
                    >{{ dict.name }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="驳回原因" prop="auditFailCause2">
                <el-input
                  v-model="form2.auditFailCause2"
                  placeholder="请输入驳回原因"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <div>
          <el-button
            type="primary"
            v-if="itemAudit.auditStatus1 == 0"
            v-hasRole="['fuzong']"
            @click="submitForm1"
            >副总经理提交审核</el-button
          >
          <el-button
            type="primary"
            v-if="itemAudit.auditStatus2 == 0"
            v-hasRole="['manager']"
            @click="submitForm2"
            >总经理提交审核</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
        <div>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  ReimburseInfo,
  delReimburse,
  getAreas,
  getUserId,
  shez1,
  shez2,
} from "@/api/zhao/work.js";

export default {
  name: "Role",
  dicts: ["work_status", "work_type"],
  data() {
    return {
      auditStatuslist:[
      {
          id: 0,
          name: "未审核",
        },
      {
          id: 1,
          name: "审核通过",
        },
        {
          id: 2,
          name: "审核驳回",
        },
      ],
      workTypelist: [
        {
          id: 1,
          name: "当天录",
        },
        {
          id: 2,
          name: "补录",
        },
      ],
      auditList: [
        {
          id: 1,
          name: "审核通过",
        },
        {
          id: 2,
          name: "审核驳回",
        },
      ],
      remark: [],
      allList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openone: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: undefined,
        areaId: undefined,
        applyTime: undefined,
        userId: undefined,
      },
      // 表单参数
      form: {},
      form1: {
        keyId: "",
        auditStatus1: "",
        auditFailCause1: "",
      },
      form2: {
        keyId: "",
        auditStatus2: "",
        auditFailCause2: "",
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "职位名称不能为空", trigger: "blur" },
        ],
      },
      rules1: {
        auditStatus1: [
          { required: true, message: "请选择审核状态", trigger: "blur" },
        ],
        auditFailCause1: [
          { required: false, message: "请输入驳回原因", trigger: "blur" },
        ],
      },
      rules2: {
        auditStatus2: [
          { required: true, message: "请选择审核状态", trigger: "blur" },
        ],
        auditFailCause2: [
          { required: false, message: "请输入驳回原因", trigger: "blur" },
        ],
      },
      itemAudit: {},
      childrenList: [],
    };
  },
  created() {
    this.getList();
    this.getAlllist();
  },
  methods: {
    audit(e, i) {
      if (e == 2 && i == 1) {
        this.rules1.auditFailCause1[0].required = true;
      } else {
        this.rules1.auditFailCause1[0].required = false;
      }

      if (e == 2 && i == 2) {
        this.rules2.auditFailCause2[0].required = true;
      } else {
        this.rules2.auditFailCause2[0].required = false;
      }
    },
    getAlllist() {
      getAreas().then((res) => {
        this.remark = res.data;
      });
      getUserId({ codes: "cehui" }).then((res) => {
        this.allList.push(...res.data);
        getUserId({ codes: "sheji" }).then((res) => {
          this.allList.push(...res.data);
        });
      });
    },
    getList() {
      this.loading = true;
      getList(this.queryParams).then((res) => {
        this.roleList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams.areaName = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleLook(row) {
      console.log(row, "123123");
      this.childrenList = row.itemDateWorkList;
      this.total = this.childrenList.length;
      this.openone = true;
    },
    handleUpdate(row) {
      const postId = row.id || this.ids;
      ReimburseInfo({ id: postId }).then((res) => {
        this.form = res.data;
        this.itemAudit = res.data.itemAudit;
        this.childrenList = res.data.itemDateWorkList;
        this.open = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let postIds = "";
      if (row.id) {
        postIds = [row.id];
      } else {
        postIds = this.ids;
      }
      this.$modal
        .confirm('是否确认删除编号为"' + postIds + '"的数据项？')
        .then(function () {
          delReimburse({ ids: postIds }).then((res) => {});
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "item/date/export",
        {
          ...this.queryParams,
        },
        `日工作列表.xlsx`
      );
    },
    cancel() {
      this.open = false;
    },
    submitForm1() {
      this.$refs["form1"].validate((valid) => {
        if (valid) {
          this.form1.keyId = this.form.id;
          shez1(this.form1).then((res) => {
            this.open = false;
            this.getList();
          });
        }
      });
    },
    submitForm2() {
      this.$refs["form2"].validate((valid) => {
        if (valid) {
          this.form2.keyId = this.form.id;
          shez2(this.form2).then((res) => {
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.text {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
}
</style>
